function MyRssReader() {
  this.feed = {};
}

MyRssReader.prototype.timeToPrettyString = function(B) {
	if (isNaN(B)) {
    return __MSG_indeterminate__
	}
	time = (new Date().getTime() - B) / 1000;
	if (time < 60) {
    return __MSG_less_than__ + __MSG_minute_ago
	} else {
    if (time < 120) {
 	  	return "about a minute ago"
    } else {
        if (time < 3600) {
	        var A = Math.round(time / 60);
	        return "about " + A + " minutes ago"
        } else {
	        if (time < 7200) {
            return "about an hour ago"
	        } else {
            if (time < 86400) {
         	  	var A = Math.round(time / 3600);
      	     	return "about " + A + " hours ago"
            } else {
	            if (time < 172800) {
                return "about a day ago"
	            } else {
                if (time < 2592000) {
	                var A = Math.round(time / 86400);
	                return "about " + A + " days ago"
                } else {
	                if (time < 5184000) {
                    return "about a month ago"
	                } else {
	                	
                 	   var A = Math.round(time / 2592000);
                    return "about " + A + " months ago"
	                }
                }
	            }
            }
	        }
        }
    }
	}
}

MyRssReader.prototype.renderFeed = function(feed) {
	this.feed = feed;
	gadgets.window.setTitle("RSS: " + feed.Title);
	var feedEl = _gel("FeedReader");
	feedEl.innerHTML = '';
	var ul = document.createElement('ul');
	if(views == "Normal") ul.className = 'FeedNormal';
	else ul.className = 'FeedMag';
	
	var numDisplay = entries + entryCount;
	if(numDisplay >= totalEntries) numDisplay = totalEntries;
	var lessEnties = 0;
	// Access the data for a given entry
	if (feed.Entry) {
		for (var i = entryCount; i < (numDisplay); i++) {
			if(feed.Entry[i] == undefined) {
				lessEnties = i;									
				break;
			}
	    var li = document.createElement('li');
        
	    if(i%2 != 0) li.className = 'OddFeed';
	    else li.className = 'EvenFeed';
			if(i == entryCount && views == "Magazine") li.className = li.className + " First";

	    var itemThumbnail = document.createElement('div');
			itemThumbnail.className = 'FeedThumbnail';
        
	    // get images from description				
			var images = feed.Entry[i].Summary;
			if(images.indexOf("<img") > 0) {
			 images = images.substring(images.indexOf("<img"),images.length);
			 images = images.substring(0,images.indexOf(">")+1);
			 images = images.substring(images.indexOf("src=\'")+5,images.length);
			 images = images.substring(0,images.indexOf("\'"));
			} else images = null;
														
			var itemImages = document.createElement('img');
			itemImages.className = 'thumb';
			if(images == null) itemImages.src = 'http://my-rss-reader.googlecode.com/svn/trunk/MyRssReader/default.png';
			else itemImages.src = images;
						
			itemThumbnail.appendChild(itemImages);

			var itemTitle	= document.createElement('h3');
			itemTitle.className = 'FeedTitle';
			itemTitle.innerHTML = feed.Entry[i].Title;
						
			var itemDate = document.createElement('small');
			itemDate.className = 'FeedDate';
			itemDate.innerHTML = ' - ' + this.timeToPrettyString(feed.Entry[i].Date);
			itemTitle.appendChild(itemDate);

			var itemDesc = document.createElement('p');
			itemDesc.className = 'FeedDesc';
			itemDesc.innerHTML = feed.Entry[i].Summary;
						
			var itemAction = document.createElement('div');
			itemAction.className = 'FeedAction';
						
			var itemShare = document.createElement('a');
			itemShare.href = 'javascript: myRssReader.share(' + i + ')';
			itemShare.innerHTML = 'Share';
						
			var spanAction = document.createElement('span');
			spanAction.innerHTML = ' | ';
						
			var itemLink = document.createElement('a');
			itemLink.href = feed.Entry[i].Link;
			itemLink.innerHTML = 'View link&raquo;';
			itemLink.target = '_blank';
						
			itemAction.appendChild(itemShare);
			itemAction.appendChild(spanAction);
			itemAction.appendChild(itemLink);
							
			li.appendChild(itemThumbnail);
			li.appendChild(itemTitle);
			li.appendChild(itemDesc);
			li.appendChild(itemAction);
						
			ul.appendChild(li);
    }

		feedEl.appendChild(ul);
            
    var feedPage = document.createElement('div');
    feedPage.className = 'FeedPager';
            
    var feedBack = document.createElement('a');
    feedBack.className = 'FeedPrev';
    feedBack.innerHTML = 'back';
    feedBack.href = 'javascript: myRssReader.back()';
		if(entryCount <= 0) feedBack.style.display = 'none';
    else feedBack.style.display = 'block';
            
		var feedNext = document.createElement('a');
    feedNext.className = 'FeedNext';
    feedNext.innerHTML = 'next';
    feedNext.href = 'javascript: myRssReader.next()';
    if(numDisplay == totalEntries) feedNext.style.display = 'none';
		if(lessEnties != 0 && numDisplay > lessEnties) {
			feedNext.style.display = 'none';
		}
    else feedNext.style.display = 'block';
            
    feedPage.appendChild(feedBack);
    feedPage.appendChild(feedNext);
            
		feedEl.appendChild(feedPage);
	}
  else {
    document.write("No feed found at " + feedurl);
	}
	gadgets.window.adjustHeight();
}

MyRssReader.prototype.generateLinkContent = function(i) {
  return "<a href='" + this.feed.Entry[i].Link + "' target='_blank'>view link &raquo;</a>";
}

MyRssReader.prototype.refreshFeed = function() {
	if(views == 'Magazine') {
		myRssReader.navigateTo('canvas');
		entries = 4;	
	}
	else myRssReader.navigateTo('home');
	_IG_FetchFeedAsJSON(prefs.getString("rssurl"), function(feed) {myRssReader.renderFeed(feed);}, totalEntries, true);
}

MyRssReader.prototype.share = function(entryId) {
  var activity = opensocial.newActivity({ 'title' : this.feed.Entry[entryId].Title,
    'body' : "share the link " + this.feed.Entry[entryId].Title + ": " + this.feed.Entry[entryId].Link,
	'url': this.feed.Entry[entryId].Link});

  opensocial.requestCreateActivity(activity, "HIGH", myRssReader.shared);
}

MyRssReader.prototype.shared = function() {
  var miniMsg = new gadgets.MiniMessage();
  miniMsg.createTimerMessage('Successfully shared.', 7);
}

MyRssReader.prototype.next = function() {
	if(views == 'Magazine') entryCount++;
	else entryCount += entries;
  myRssReader.renderFeed(this.feed);
}

MyRssReader.prototype.back = function() {
	if(views == 'Magazine') entryCount--; 	
	else entryCount -= entries;
 	if(entryCount < 0) entryCount = 0;
 	myRssReader.renderFeed(this.feed);
}

MyRssReader.prototype.navigateTo = function(dest) {
    var supported_views = gadgets.views.getSupportedViews();
    gadgets.views.requestNavigateTo(supported_views[dest]);
};

